CODE 51. Remove Duplicates from Sorted List

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/09/29/2013-09-29-CODE 51 Remove Duplicates from Sorted List/

访问原文「CODE 51. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ListNode deleteDuplicates(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if (null == head) {
return null;
}
ListNode node = new ListNode(head.val);
ListNode next = head.next;
ListNode currentNode = node;
while (null != next) {
if (next.val > currentNode.val) {
currentNode.next = new ListNode(next.val);
currentNode = currentNode.next;
} else {
next = next.next;
}
}
return node;
}
Jerky Lu wechat
欢迎加入微信公众号